<?php
namespace App\Service\Member;

use Illuminate\Support\Facades\DB;

class DrugService extends BaseService
{
    //药品

    //选项
    public function getOption()
    {
        $arys = ['cate' => []];
        $cateOnes = $this->dbConn($this->dbDrugCate)
            ->where('del', 0)
            ->get();
        if (count($cateOnes)) {
            foreach ($cateOnes as $v) {
                $v = (array) $v;
                $arys['cate'][] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                ];
            }
        }
        return $arys;
    }

    /***
     * 列表
     * @param array $pages【start、limit】
     * @param array $pas【cate_id、kword】
     */
    public function getList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDrug);
        if ($pas['cate_id']) {
            $db = $db->where('cate_id', $pas['cate_id']);
        }
        if ($pas['kword']) {
            $db = $db->where('title', 'like', $pas['keord'].'%');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback();
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $img = file_exists($v['img']) ? env('DOMAIN').'/'.$v['img'] : '';
//            $symptom = $v['symptom'] ? explode('||', $v['symptom']) : [];
            $tagOnes = $this->dbConn($this->dbDrugTag)
                ->wher('del', 0)
                ->wher('drug_id', $v['id'])
                ->get();
            $tagList = [];
            if (count($tagOnes)) {
                foreach ($tagOnes as $tv) {
                    $tagList[] = $tv->title;
                }
            }
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'img' => $img,
//                'symptom' => $symptom,
                'symptom' => $tagList,
                'price' => toNumber($v['price']),
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 详情
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbDrug)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $img = file_exists($res['img']) ? env('DOMAIN').'/'.$res['img'] : '';
//        $symptom = $res['symptom'] ? explode('||', $res['symptom']) : [];
        $tagOnes = $this->dbConn($this->dbDrugTag)
            ->wher('del', 0)
            ->wher('drug_id', $res['id'])
            ->get();
        $tagList = [];
        if (count($tagOnes)) {
            foreach ($tagOnes as $tv) {
                $tagList[] = $tv->title;
            }
        }
        $arys = [
            'id' => $res['id'],
            'title' => $res['title'],
            'img' => $img,
//            'symptom' => $symptom,
            'symptom' => $tagList,
            'price' => toNumber($res['price']),
            'num' => $res['num'],
            'zhuzhi' => $res['zhuzhi'],
            'yongfa' => $res['yongfa'],
            'period' => $res['period'],
            'instruction' => $res['instruction'],
        ];
        return $this->toback(0, '', $arys);
    }

    /**
     * 下单
     * @param int $drug_id
     * @param array $pas【drug_id，num，address_id，pstime】
     */
    public function orderAdd($pas)
    {
        $drugOne = (array) $this->dbConn($this->dbDrug)
            ->where('del', 0)
            ->where('id', $pas['drug_id'])
            ->first();
        if (empty($drugOne)) {
            return $this->toback(-2, '没有药品');
        }
        if ($drugOne['num'] < $pas['num']) {
            return $this->toback(-2, '药品库存不足');
        }
        $addressOne = (array) $this->dbConn($this->dbUaddress)
            ->where('del', 0)
            ->where('id', $pas['address_id'])
            ->first();
        if (empty($addressOne)) {
            return $this->toback(-2, '没有收货地址');
        }
        $pas['orderno'] = toOrder();
        $pas['price'] = $drugOne['price'] * $pas['num'];
        $pas['addtime'] = toDate();
        DB::beginTransaction(); //开启事务
        try {
            //下单
            $lastid = $this->dbConn($this->dbDrugOrder)->insertGetId($pas);
            if (!$lastid) {
                return $this->toback(-1, 'error');
            }
            //减库存
            $up = $this->dbConn($this->dbDrug)->where('id', $pas['drug_id'])
                ->decrement('num', $pas['num']);
            if (!$up) {
                return $this->toback(-1, 'error');
            }
            $resData = [
                'yw_type' => 6,
                'yw_id' => $lastid,
            ];
            //更新订单状态
            DB::commit(); //提交事务
            return $this->toback(0, '', $resData);
        } catch (\Exception $e) {
            DB::rollBack(); //事务回滚
            echo $e->getMessage();
            return $this->toback(-1, 'error');
        }
    }
}